package com.oa.srms.apps.business.student.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.oa.srms.apps.business.student.IAvatarInfoService;
import com.oa.srms.apps.business.student.IMailInfoService;
import com.oa.srms.apps.business.student.IMajorInfoService;
import com.oa.srms.apps.business.student.IStudentDetailService;
import com.oa.srms.apps.business.student.IStudentInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.constants.IStudentStates;
import com.oa.srms.apps.dao.student.IStudentInfoDao;
import com.oa.srms.apps.entity.student.StudentDetailValue;
import com.oa.srms.apps.entity.student.StudentInfoValue;

public class StudentInfoServiceImpl implements IStudentInfoService {
	
	@Autowired
	private IStudentDetailService studentDetailService;
	@Autowired
	private IMailInfoService mailInfoService;
	@Autowired
	private IMajorInfoService majorInfoService;
	@Autowired
	private IAvatarInfoService avatarInfoService;
	@Autowired
	private StudentInfoValue studentInfoValue;
	@Autowired
	private StudentDetailValue studentDetailValue;
	@Autowired
	private IStudentInfoDao studentInfoDao;
	
	@Override
	public Integer addStudentInfo(String stuNo, String stuName,
			int studentId, int mailId, int avatarId) throws Exception {
		studentInfoValue.setStuNo(stuNo);
		studentInfoValue.setStuName(stuName);
		studentInfoValue.setStuPwd(this.getPassword());
		studentInfoValue.setStuDetail(studentDetailService.queryById(studentId));
		studentInfoValue.setMailInfo(mailInfoService.queryByMailId(mailId));
		studentInfoValue.setAvatarInfo(avatarInfoService.queryByAvatarId(avatarId));
		studentInfoValue.setStates(IStudentStates.CREATE);
		return studentInfoDao.addStudentInfo(studentInfoValue);
	}
	
	private String getPassword() {
		// 随机产生一个6位的密码
		String sourcenum = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String siglenum = "";
		// 该变量保存验证码
		String password = "";
		for (int i = 0; i < 6; i++) {
			int index = (int) (Math.random() * 100) % 61;
			siglenum = sourcenum.substring(index, index + 1);
			password += siglenum;
		}
		return password;
	}

	@Override
	public void modifyStudentInfo(int id, String stuNo, String stuName, String stuPwd) throws Exception {
		studentInfoValue=this.queryById(id);
		if(studentInfoValue!=null){
			if(stuNo != null)
			studentInfoValue.setStuNo(stuNo);
			if(stuName != null)
			studentInfoValue.setStuName(stuName);
			if(stuPwd != null)
			studentInfoValue.setStuPwd(stuPwd);
			studentInfoDao.editStudentInfo(studentInfoValue);
		}
	}

	@Override
	public void deleteStudentInfo(int id) throws Exception {
		studentInfoValue=studentInfoDao.queryById(id);
		studentInfoValue.setStates(IDEDStates.DELETE);
		studentInfoDao.editStudentInfo(studentInfoValue);
	}
	
	@Override
	public void setStudentStates(Integer id, Integer states) throws Exception {
		studentInfoValue=studentInfoDao.queryById(id);
		studentInfoValue.setStates(states);
		studentInfoDao.editStudentInfo(studentInfoValue);
	}

	@Override
	public StudentInfoValue queryById(Integer id) throws Exception {
		return studentInfoDao.queryById(id);
	}

	@Override
	public List<StudentInfoValue> queryStudentInfo(String stuNo,
			String stuName,String gender, Integer majorId, Integer grade, Integer classNo, Integer states,
			Integer rows, Integer pageNo) throws Exception {
		if(grade != null)
			studentDetailValue.setGrade(grade);
		studentDetailValue.setClassNo(classNo);
		studentDetailValue.setGender(gender);
		if(majorId != null)
		studentDetailValue.setMajorInfo(majorInfoService.queryByMajorId(majorId));
		return studentInfoDao.queryStudentInfo(stuNo, stuName, states, studentDetailValue, rows, pageNo);
	}

	@Override
	public Integer getTotalPageNumber(String stuNo, String stuName, String gender, Integer majorId, Integer grade, Integer classNo, Integer states, Integer rows) throws Exception {
		studentDetailValue.setGrade(grade);
		studentDetailValue.setClassNo(classNo);
		studentDetailValue.setGender(gender);
		if(majorId != null)
		studentDetailValue.setMajorInfo(majorInfoService.queryByMajorId(majorId));
		return studentInfoDao.getTotalPageNumber(stuNo, stuName, states, studentDetailValue, rows);
	}

	@Override
	public Integer getTotalNumber() {
		return studentInfoDao.getTotalNumber();
	}

	@Override
	public StudentInfoValue queryByStuNo(String stuNo) throws Exception {
		return studentInfoDao.queryByStuNo(stuNo);
	}

	@Override
	public List<StudentInfoValue> queryStudentInfoForSendMail(
			Integer studentState, Integer mailState) throws Exception {
		return studentInfoDao.queryStudentInfoForSendMail(studentState, mailState);
	}

	@Override
	public List<StudentInfoValue> queryStudentInfo(Integer states)
			throws Exception {
		return studentInfoDao.queryStudentInfo(states);
	}

	@Override
	public List<StudentInfoValue> queryStudentInfoAboveStates(String stuNo,
			String stuName, String gender, Integer majorId, Integer grade,
			Integer classNo, Integer states, Integer rows, Integer pageNo)
			throws Exception {
		studentDetailValue.setGrade(grade);
		studentDetailValue.setClassNo(classNo);
		studentDetailValue.setGender(gender);
		if(majorId != null)
		studentDetailValue.setMajorInfo(majorInfoService.queryByMajorId(majorId));
		return studentInfoDao.queryStudentInfoAboveStates(stuNo, stuName, states, studentDetailValue, rows, pageNo);
	}

	@Override
	public Integer getTotalPageNumberAboveStates(String stuNo, String stuName,
			String gender, Integer majorId, Integer grade, Integer classNo,
			Integer states, Integer rows) throws Exception {
		studentDetailValue.setGrade(grade);
		studentDetailValue.setClassNo(classNo);
		studentDetailValue.setGender(gender);
		if(majorId != null)
		studentDetailValue.setMajorInfo(majorInfoService.queryByMajorId(majorId));
		return studentInfoDao.getTotalPageNumberAboveStates(stuNo, stuName, states, studentDetailValue, rows);
	}

	@Override
	public List<StudentInfoValue> queryStudentInfo(String gender,
			Integer majorId, Integer grade) throws Exception {
		studentDetailValue.setGrade(grade);
		studentDetailValue.setGender(gender);
		if(majorId != null)
		studentDetailValue.setMajorInfo(majorInfoService.queryByMajorId(majorId));
		return studentInfoDao.queryStudentInfo(studentDetailValue);
	}


}
